package org.yaac.client.activity;

import javax.inject.Inject;

import org.yaac.client.place.OverviewPlace;
import org.yaac.client.service.OverviewServiceAsync;
import org.yaac.client.ui.Overview;
import org.yaac.client.util.ExceptionManager;
import org.yaac.shared.cap.CapSummaryDTO;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class OverviewActivity extends SinglePlaceActivity<OverviewPlace> {

	private final Overview view;
	
	private final OverviewServiceAsync overviewService;
	
	private final ExceptionManager exceptionManager;
	
	@Inject
	OverviewActivity(Overview view, 
			OverviewServiceAsync overviewService, 
			ExceptionManager exceptionManager) {
		super();
		this.view = view;
		this.overviewService = overviewService;
		this.exceptionManager = exceptionManager;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		
		overviewService.loadCapSummary(new AsyncCallback<CapSummaryDTO>() {
			@Override
			public void onSuccess(CapSummaryDTO result) {
				view.updateCapabilities(result.caps());
			}
			
			@Override
			public void onFailure(Throwable caught) {
				exceptionManager.handle(caught);
			}
		});
	}

	@Override
	public SinglePlaceActivity<OverviewPlace> withPlace(OverviewPlace place) {
		return this;
	}

}
